<?php
$config = new config_edit;
require($config->root_path . "/include/function/db.php");
class udb
{
	public $sql, $name, $uid, $bc_db; 
	private $Udb, $SQLtime, $open, $r_p, $db_ID, $db_Host, $db_Username, $db_Password, $db_, $sql_s = array(), $setsql_txt, $err ,$ret_text = array(), $c_sql_ret, $sql_count_show;
	public function __construct($root_path,$dbID,$dbHost,$dbUsername,$dbPassword,$db)
	{
		$this->SQLtime = time();
		$this->accesses++;
		$this->r_p = $root_path . "/date/";
		$this->db_ID = $dbID;
		$this->db_Host = $dbHost;
		$this->db_Username = $dbUsername;
		$this->db_Password = $dbPassword;
		$this->db_ = $db;
	}
	function getSQLtime() 
	{ 
		return(date("Y-m-d H-i-s", $this->SQLtime)); 
	}
	function getSQLtime_Y_m() 
	{ 
		return(str_replace(" ","",date("Y m", $this->SQLtime))); 
	}
	function getSQLtime_Y_m_d() 
	{ 
		return(str_replace(" ","",date("Y m d", $this->SQLtime))); 
	}
	private function mdata($sql,$name,$uid,$err)
	{
		$this->sql = $sql;
		$this->name = $name;
		$this->uid = $uid;
		$this->err = $err;
		if(!is_dir($this->r_p.$this->getSQLtime_Y_m()))
		{
			@mkdir($this->r_p.$this->getSQLtime_Y_m());
			@chmod($this->r_p.$this->getSQLtime_Y_m(), 0777);
		}
		if (!file_exists($this->r_p . $this->getSQLtime_Y_m() . "/" . $this->getSQLtime_Y_m_d() . ".txt"))
		{
			$this->open=@fopen($this->r_p . $this->getSQLtime_Y_m() . "/" . $this->getSQLtime_Y_m_d() . ".txt","w");
			@fwrite($this->open, $this->getSQLtime() . "\r\n");
			@chmod($this->r_p . $this->getSQLtime_Y_m() . "/" . $this->getSQLtime_Y_m_d() . ".txt", 0777);
		}
		else
		{
			$this->open=@fopen($this->r_p . $this->getSQLtime_Y_m() . "/" . $this->getSQLtime_Y_m_d() . ".txt","a");
		}
		flock($this->open, LOCK_SH); 
		fwrite($this->open, "[Name]-> " . $this->name . "   [uid]-> " . $this->uid . "   [Operation]-> " . $this->sql . "   [Time]-> ". $this->getSQLtime() . "   [result]-> " . $this->err . "\r\n");
		flock($this->open, LOCK_UN);
		fclose($this->open);
	}

	private function sqldb($sql,$type,$name,$uid,$k=0)
	{
		if ($sql!="" && $name!=null && $uid!=null)
		{
			$this->Udb = new bc_db($this->db_ID,$this->db_Host,$this->db_Username,$this->db_Password,$this->db_);
			$this->setsql_txt = $this->Udb->setsql($sql,$type,$k);
			//$this->mdata($sql,$name,$uid,$_SERVER["REMOTE_ADDR"]);
			return $this->setsql_txt;
		}
		else
		return false;
	}
	
	function sqls($sql,$k=0)
	{
		$this->ret_text = null;
		if (!$_SESSION["lname"]) $this->name="Tourists";
		else $this->name=$_SESSION["lname"];
		if (!$_SESSION["luid"]) $this->uid="0";
		else $this->uid=$_SESSION["luid"];
		$this->sql_s = explode('##',$sql);
		$this->sql_count_show = count($this->sql_s);
		
		if ($this->sql_count_show > 1)
		{
			for ($i=0;$i<$this->sql_count_show-1;$i++)
			$this->ret_text[$i] = $this->sqldb($this->sql_s[$i],MYSQL_ASSOC,$this->name,$this->uid,0);
			$this->ret_text[$this->sql_count_show-1] = $this->sqldb($this->sql_s[$this->sql_count_show-1],MYSQL_ASSOC,$this->name,$this->uid,$k);
			return $this->ret_text;
		}
		else return $this->sqldb($this->sql_s[count($this->sql_s)-1],MYSQL_ASSOC,$this->name,$this->uid,$k);
		
	}
	function c_sql($where,$k=0)
	{
		$this->c_sql_ret = $this->sqls($where,$k);
		if ($this->c_sql_ret!=false)
		{
			foreach($this->c_sql_ret as $key=>$value)
			{
				return $value[col];
			}
		}
	}
}
$udb = new udb($config->root_path,$config->dbID,$config->dbHost,$config->dbUsername,$config->dbPassword,$config->db);
?>